package com.qf.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.pojo.*;
import com.qf.service.*;
import com.qf.vo.GoodsAllinone;
import com.qf.vo.GoodsCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.serviceimpl
 * @Description:
 * @Date 2022/10/13 20:16
 */
@SuppressWarnings("all")
@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {

    @Autowired
    DtsGoodsMapper dtsGoodsMapper;
    @Autowired
    DtsGoodsProductService productService;

    @Autowired
    DtsGoodsSpecificationService specificationService;

    @Autowired
    DtsGoodsAttributeService attributeService;

    @Autowired
    DtsCategoryService dtsCategoryService;
    @Override
    public long getCount() {
        LambdaQueryWrapper<DtsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsGoods::getDeleted,false);

        return dtsGoodsMapper.selectCount(wrapper);
    }

    /**
     * 分页查找
     */
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String goodsSn, String name, List<DtsBrand> brands) {

        IPage<DtsGoods> iPage = new Page<>(page,limit);
        QueryWrapper<DtsGoods> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(goodsSn)){
            wrapper.like("goods_sn",goodsSn);
        }
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }

        if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else{
            wrapper.orderByAsc(sort);
        }

        if(brands!=null && brands.size()>0){
            wrapper.in("brand_id",brands);
        }
        wrapper.eq("deleted", false);
        //分页查询所有
        dtsGoodsMapper.selectPage(iPage,wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items",iPage.getRecords());
        return map;
    }

    @Override
    public void insert(GoodsAllinone goodsAllinone) {

        DtsGoods goods = goodsAllinone.getGoods();
        //新增商品
        //判断商品编号和商品名称 是否已经存在
        LambdaQueryWrapper<DtsGoods> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DtsGoods::getGoodsSn, goods.getGoodsSn());
        Integer count1 = dtsGoodsMapper.selectCount(wrapper1);
        if(count1 > 0) {
            throw new RuntimeException("商品编号已存在");
        }

        LambdaQueryWrapper<DtsGoods> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(DtsGoods::getName, goods.getName());
        Integer count2 = dtsGoodsMapper.selectCount(wrapper2);
        if(count2 > 0) {
            throw new RuntimeException("商品名称已存在");
        }

        //新增商品
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        goods.setDeleted(false);
        dtsGoodsMapper.insert(goods);

        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        for (DtsGoodsProduct product : products) {
            product.setGoodsId(goods.getId());
            productService.insert(product);
        }

        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            attributeService.insert(attribute);
        }


        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        for (DtsGoodsSpecification specification : specifications) {
            specification.setGoodsId(goods.getId());
            specificationService.insert(specification);
        }
    }

    /**
     * 修改回显
     * @param goodsId
     * @return
     */

    @Override
    public Map<String, Object> detail(Integer goodsId) {
        //查询商品
        DtsGoods goods = dtsGoodsMapper.selectById(goodsId);
        //查询商品的分类
        List<Integer> categoryIds = dtsCategoryService.findByGoodsId(goods.getCategoryId());
        //查询商品的属性
        List<DtsGoodsAttribute> attributes = attributeService.findByGoodsId(goodsId);
        //查询商品的规格
        List<DtsGoodsSpecification> specifications = specificationService.findByGoodsId(goodsId);
        //查询商品的库存
        List<DtsGoodsProduct> products =productService.findByGoodsId(goodsId);


        Map<String,Object> map = new HashMap<>();
        map.put("categoryIds",categoryIds);
        map.put("goods",goods);
        map.put("attributes",attributes);
        map.put("specifications",specifications);
        map.put("products",products);

        return map;
    }

    /**
     *修改保存
     */

    @Override
    public void update(GoodsAllinone goodsAllinone) {

//        获取商品对象
        DtsGoods goods = goodsAllinone.getGoods();
        goods.setUpdateTime(new Date());
        //更新商品对象
        dtsGoodsMapper.updateById(goods);

        //删除商品属性
        attributeService.delete(goods.getId());
        //删除商品规格
        specificationService.delete(goods.getId());
        //删除商品库存
        productService.delete(goods.getId());

        //获取商品属性
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
          //新增商品属性
        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            attributeService.insert(attribute);
        }
        //获取商品规格
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
            //新增商品规格
        for (DtsGoodsSpecification specification : specifications) {
            specification.setGoodsId(goods.getId());
            specificationService.insert(specification);
        }
        //获取商品库存
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        for (DtsGoodsProduct product : products) {
            product.setGoodsId(goods.getId());
            productService.insert(product);
        }
    }

    /**
     * 删除商品
     */

    @Override
    public void delete(DtsGoods dtsGoods) {
        //删除商品
        dtsGoodsMapper.deleteById(dtsGoods.getId());
        //删除属性
        attributeService.delete(dtsGoods.getId());
        //删除规格
        specificationService.delete(dtsGoods.getId());
        //删除库存
        productService.delete(dtsGoods.getId());
    }

    @Override
    public List<GoodsCountVo> findCount() {
        return dtsGoodsMapper.findCount();
    }

    @Override
    public DtsGoods findByGoodsId(Long goodsId) {
        LambdaQueryWrapper<DtsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsGoods::getId,goodsId);
        wrapper.eq(DtsGoods::getDeleted,false);

        return dtsGoodsMapper.selectOne(wrapper);
    }
}
